/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package codeGenerate.factory;

import codeGenerate.CommonPageParser;
import codeGenerate.CreateBean;
import codeGenerate.def.CodeResourceUtil;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.VelocityContext;

public class CodeGenerateFactory {
	private static final Log log = LogFactory.getLog(CodeGenerateFactory.class);
	private static String url = CodeResourceUtil.URL;
	private static String username = CodeResourceUtil.USERNAME;
	private static String passWord = CodeResourceUtil.PASSWORD;
	private static String buss_package = CodeResourceUtil.bussiPackage;

	private static String projectPath = getProjectPath();

	public static void codeGenerate(String tableName, String codeName, String entityPackage, String keyType) {
		CreateBean createBean = new CreateBean();

		createBean.setDbInfo(url, username, passWord);

		String className = createBean.getTablesNameToClassName(tableName);
		String lowerName = className.substring(0, 1).toLowerCase() + className.substring(1, className.length());

		String srcPath = projectPath + CodeResourceUtil.source_root_package + "\\";

		String pckPath = srcPath + CodeResourceUtil.bussiPackageUrl + "\\";

		String webPath = projectPath + CodeResourceUtil.web_root_package + "\\view\\" + CodeResourceUtil.bussiPackageUrl
				+ "\\";

		// String modelPath = "page\\" + entityPackage + "\\" + className +
		// "Page.java";
		String beanPath = "domain\\" + entityPackage + "\\" + className + ".java";
		String mapperPath = "mapper\\" + entityPackage + "\\" + className + "Mapper.java";
		String servicePath = "service\\" + entityPackage + "\\" + className + "Service.java";
		String serviceImplPath = "service\\" + entityPackage + "\\impl\\" + className + "ServiceImpl.java";
		String controllerPath = "controller\\" + entityPackage + "\\" + className + "Controller.java";
		String sqlMapperPath = "mapper\\" + entityPackage + "\\" + className + "Mapper.xml";
		webPath = webPath + entityPackage + "\\";

		 //String jsonPath = lowerName + ".html";
		// String jsPath = "page-" + lowerName + ".js";

		VelocityContext context = new VelocityContext();
		context.put("className", className);
		context.put("lowerName", lowerName);
		context.put("codeName", codeName);
		context.put("tableName", tableName);
		context.put("bussPackage", buss_package);
		context.put("entityPackage", entityPackage);
		context.put("keyType", keyType);
		try {
			context.put("feilds", createBean.getBeanFeilds(tableName));
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			Map<String, Object> sqlMap = createBean.getAutoCreateSql(tableName);
			context.put("columnDatas", createBean.getColumnDatas(tableName));
			context.put("SQL", sqlMap);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		CommonPageParser.WriterPage(context, "DomainTemplate.ftl", pckPath, beanPath);
		// CommonPageParser.WriterPage(context, "PageTemplate.ftl", pckPath,modelPath);
		CommonPageParser.WriterPage(context, "MapperTemplate.ftl", pckPath, mapperPath);
		CommonPageParser.WriterPage(context, "ServiceTemplate.ftl", pckPath, servicePath);
		CommonPageParser.WriterPage(context, "ServiceImplTemplate.ftl", pckPath, serviceImplPath);
		CommonPageParser.WriterPage(context, "MapperTemplate.xml", pckPath, sqlMapperPath);
		CommonPageParser.WriterPage(context, "ControllerTemplate.ftl", pckPath, controllerPath);

		// CommonPageParser.WriterPage(context, "jspTemplate.ftl", webPath,
		// jspPath);
		// CommonPageParser.WriterPage(context, "jsTemplate.ftl", webPath,
		// jsPath);

		log.info("----------------------------end---------------------------");
	}

	public static String getProjectPath() {
		String path = System.getProperty("user.dir").replace("\\", "/") + "/";
		return path;
	}
}